<?php

namespace App\Http\Controllers;

use App\Item;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesResources;
use Illuminate\Support\Facades\Cache;

class Controller extends BaseController
{
    public function __construct()
    {
        if(Cache::has('nav')){
            $navs = Cache::get('nav');
        } else {
            $items = config('site.items');
            foreach ($items as $itemName => $item) {
                $navs[$itemName]['nav'] = $item['nav'];
                $navs[$itemName]['name'] = $item['name'];
                $navs[$itemName]['itemName'] = $itemName;
                $navs[$itemName]['nav'] = $item['nav'];
                if (1 == $item['nav']) {
                    if (isset($item['type'])) {
                        echo '</br>'.$item['name'];
                        print_r($item['data'][$item['type']]['type'] );
                        if ($item['data'][$item['type']]['type'] == "dynamic") {
                            $navs[$itemName]['list'] = Item::where('type', $item['data'][$item['type']]['value'])->lists('name', 'id');
                        } else {
                            $navs[$itemName]['list'] = $item['data'][$item['type']]['value'];
                        }
                    } else {
                        $navs[$itemName]['list'] = Item::where('type', $itemName)->lists('name', 'id');
                    }
                }
            }
            Cache::forever('nav',$navs);
        }
        view()->share('navs',$navs);
    }

    use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests;
}
